package com.augurit.gzcgwsp.csshlj.dwr;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.augurit.gzcgwsp.common.dwr.BaseDwrService;
import com.augurit.gzcgwsp.csshlj.entity.Person;
import com.augurit.gzcgwsp.csshlj.service.IPersonService;
import com.augurit.gzcgwsp.excelimportexport.util.FileUtil;
import com.augurit.gzcgwsp.excelimportexport.util.GetFileName;
@Controller
@RemoteProxy
/**
 * 城市生活垃圾dwr
 */
public class CsshljCntl extends BaseDwrService{
	private final static Logger log=Logger.getLogger(CsshljCntl.class);
	@Autowired
	private IPersonService personService;// 垃圾处置专业人员
	
	/**
	 * 城市生活垃圾专业人员修改
	 * 
	 * @param params
	 * @param uploadFile
	 * @return
	 * @throws Exception
	 */
	@RemoteMethod
	public String updateShljPerson(String params, InputStream jlfile,InputStream xlfile)
			throws Exception {
		    JSONObject json=JSONObject.fromObject(params);
		    String jlfileName=json.getString("jlfileName");
		    String xlfileName=json.getString("xlfileName");
		    Person person = (Person) JSONObject.toBean(json, Person.class);
		    Map msgMap=new HashMap();
		    WebContext ctx = WebContextFactory.get();
            String contextPath = ctx.getHttpServletRequest().getSession()
                    .getServletContext().getRealPath("/");
            boolean flag=true;
		 try {
			 personService.updateEntity(contextPath,person,jlfile,xlfile,jlfileName,xlfileName);
			
		 } catch (Exception e) {
			flag=false;
			log.error("修改专业人员错误"+e);
		}
		 msgMap.put("msg", flag);
		return super.getJsonString(msgMap);
		// return null;
	}
	
	@RemoteMethod
	public String updateFile(String params, InputStream is) throws IOException{
		String filerealpath="Templates/csshlj/attach/";
	    WebContext ctx = WebContextFactory.get();
	    String contextPath = ctx.getHttpServletRequest().getSession()
        .getServletContext().getRealPath("/");
	    JSONObject json=JSONObject.fromObject(params);
	    String filename=json.getString("filename");
	    Map<String,String> result=new HashMap<String,String>();
		if(is!=null && is.available()>0){
			String realpath=GetFileName.GetRealPath(filerealpath,filename);
			String filepath=contextPath+realpath;
			uploadFileByStream(filepath, contextPath, is);
			result.put("path", realpath);
			result.put("filename", filename);
			return super.getJsonString(result);
		}
		return null;
	  }


		/**
		 * 流的上传文件
		 * @param filepath
		 * @param inpuStream
		 */
		private void uploadFileByStream(String filepath,String contextPath,InputStream fileStream){
			try {
				filepath=filepath.replace("/", File.separator);
				File file=new File(filepath);
				if(!file.exists()){
					String dirPaths=filepath.substring(0,filepath.lastIndexOf(File.separator));
					File dir=new File(dirPaths);
					if(!dir.exists()){
						FileUtil.createDir(dirPaths);
					}
					file.createNewFile();
				}
				FileOutputStream targetFile = new FileOutputStream(new File(
						(filepath).replace("/", File.separator)));
				byte[] size = new byte[fileStream.available()];
				fileStream.read(size);
				targetFile.write(size);
				targetFile.flush();
				targetFile.close();
				fileStream.close();
			} catch (Exception e) {
				log.info("上传附件出现错误"+e);
			} 
		}
	
}
